﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Xml;
using ELearningSystem.Gateway.Entities;
using NHibernate;
using NHibernate.Criterion;

namespace ELearningSystem.Gateway.Gateway
{
    public class ContentGateway:CommonGateway
    {
       public List<TreeContent> GetAllTreeContentsUnderNode(TreeContent parentNode)
        {
            List<TreeContent> treeContents = new List<TreeContent>();
            try
            {
                using (ISession session = createDataBaseSession.OpenSession())
                {
                    using (session.BeginTransaction())
                    {
                        var allTreeContents = session.CreateCriteria(typeof(TreeContent)).Add(Restrictions.Eq("ParentNode",parentNode)).List<TreeContent>();
                        return allTreeContents.ToList();
                    }
                }
                
            }
            catch (Exception exception)
            {
                throw new Exception("Could not found any item", exception);
            }
        }

        public TreeContent GetRoot()
        {
            
             using (ISession session = createDataBaseSession.OpenSession())
             {
                 using (session.BeginTransaction())
                 {
                     var treeContent =(TreeContent) session.CreateCriteria(typeof(TreeContent)).Add(Restrictions.IsNull("ParentNode.Id")).UniqueResult();
                     return treeContent;
                 }
             }
        }

        public TreeContent GetTreeContent(int id)
        {
            using (ISession session = createDataBaseSession.OpenSession())
            {
                using (session.BeginTransaction())
                {
                    var treeContent = session.CreateCriteria(typeof(TreeContent)).Add(Restrictions.Eq("Id",id)).UniqueResult<TreeContent>();
                    return treeContent;
                }
            }
        }
        public string GetTreeDataSource()
        {
            return GetGeneratedXML();
        }

        private string GetGeneratedXML()
        {
            TreeContent rootContent = GetRoot();
            string treexml = HttpContext.Current.Server.MapPath("tree.xml");
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(treexml, settings);
            writer.WriteStartDocument();
            writer.WriteStartElement("CATEGORY");
            writer.WriteAttributeString("ID", rootContent.Id.ToString(CultureInfo.InvariantCulture));
            writer.WriteAttributeString("name", rootContent.Text);
            writer.WriteEndElement();
            writer.Flush();
            writer.Close();
            XmlDocument document = new XmlDocument();
            document.Load(treexml);
            XmlElement rootElement = document.DocumentElement;
            XmlElement topic = document.CreateElement("TOPIC");
            XmlElement content = document.CreateElement("CONTENT");

            IList<TreeContent> topicContents = GetAllTreeContentsUnderNode(rootContent);

            foreach (var topicContent in topicContents)
            {
                rootElement = document.DocumentElement;
                topic = document.CreateElement("TOPIC");
                topic.SetAttribute("ID", topicContent.Id.ToString());
                topic.SetAttribute("name", topicContent.Text);
                rootElement.AppendChild(topic);
                IList<TreeContent> contents = GetAllTreeContentsUnderNode(topicContent);
                foreach (var aContent in contents)
                {
                    content = document.CreateElement("CONTENT");
                    content.SetAttribute("ID", aContent.Id.ToString());
                    content.SetAttribute("name", aContent.Text);
                    topic.AppendChild(content);
                }
            }
            document.Save(treexml);
            return document.InnerXml;
        }

        public Content GetContent(int treeContentId)
        {
            using (ISession session = createDataBaseSession.OpenSession())
            {
                using (session.BeginTransaction())
                {
                    var treeContent = session.CreateCriteria(typeof(TreeContent)).Add(Restrictions.Eq("Id", treeContentId)).UniqueResult<TreeContent>();
                    return treeContent.Content;
                }
            }
        }

    }
}
